package com.inspinia.base.util;

import com.inspinia.base.common.exception.BusinessException;
import com.inspinia.base.util.model.TreeGridVo;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Created by likun on 2016/12/6.
 * 把数据导出到excel中并以树状结构分级
 */
public class TreeGridExportUtil {
    /**
     * 日志对象
     */
    protected static Logger logger = LoggerFactory.getLogger(TreeGridExportUtil.class);

    private static int rowCount = 0; //表格已有多少行
//    private static List<Integer> lvls=new ArrayList<>();
//    private static List<Integer[]> rgb=new ArrayList<>();

    /**
     * 导出表格
     *
     * @param data
     * @param outputPath
     * @throws IOException
     */
    public static void export(TreeGridVo data, String outputPath) {
        rowCount=0;
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet();
        sheet.setColumnWidth(0, 10000);
//        initColor();
        setData(wb, sheet, data);
//        groupRow(sheet);
        FileUtil.createFolder(outputPath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputPath);
            wb.write(fos);

        } catch (Exception e) {
            logger.error("", e);
            throw new BusinessException("导出失败");
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }

    }

//    private static void initColor() {
//        for (int i=0; i<100; i++){
//            rgb.add(new Integer[]{getRandomInt(),getRandomInt(),getRandomInt()});
//        }
//    }

//    private static void groupRow(XSSFSheet sheet) {
//        if (lvls.size()>0){
//            for (int i=0; i<lvls.size(); i++){
//                for (int j=i+1; j<lvls.size(); j++){
//                    if (lvls.get(i)==lvls.get(j)) break;
//                    if(lvls.get(i)<lvls.get(j)){
//                        if (j+1<lvls.size()){
//                            if(lvls.get(j)!=lvls.get(j+1)){
////                                sheet.groupRow(i+1,j+1);
//                                System.out.println((i+1)+","+(j+1));
//                            }
//                        }else {
////                            sheet.groupRow(i+1,j+1);
//                            System.out.println((i+1)+","+(j+1));
//                        }
//                    }
//                }
//            }
//        }
//    }

    /**
     * 设置表格数据
     *
     * @param wb
     * @param sheet
     * @param data
     */
    private static void setData(XSSFWorkbook wb, XSSFSheet sheet, TreeGridVo data) {
        setTitle(wb, sheet, data);
        if (data.getChildren() != null && data.getChildren().size() != 0) {
            eachData(wb, sheet, data.getChildren(), 1);
        }
    }

    /**
     * 遍历所有的data并创建表格
     *
     * @param wb
     * @param sheet
     * @param dataList
     * @param lvl
     */
    private static void eachData(XSSFWorkbook wb, XSSFSheet sheet, List<TreeGridVo> dataList, int lvl) {
        for (TreeGridVo tree : dataList) {
            if (tree.getRow() != null && tree.getRow().size() != 0) {
//                lvls.add(lvl);
                XSSFRow row = sheet.createRow(rowCount + 1);
                for (int i = 0; i < tree.getRow().size(); i++) {
                    String spaces = "";
                    if (i == 0) {
                        for (int j = 1; j < lvl; j++) {
                            spaces += "   ";
                        }
                        spaces += "▶";
                    }
                    XSSFCell cell = row.createCell(i);
                    cell.setCellValue(spaces + tree.getRow().get(i));
//                    setCellBackgroundColor(wb,cell,rgb.get(lvl)[0],rgb.get(lvl)[1],rgb.get(lvl)[2]);
                }
                rowCount++;
            }
            if (tree.getChildren() != null && tree.getChildren().size() != 0) {
                eachData(wb, sheet, tree.getChildren(), lvl + 1);
            }
        }
    }

    /**
     * 设置表格的title
     *
     * @param wb
     * @param sheet
     * @param data
     */
    private static void setTitle(XSSFWorkbook wb, XSSFSheet sheet, TreeGridVo data) {
        if (data.getRow() != null && data.getRow().size() != 0) {
            XSSFRow row = sheet.createRow(0);
            int r = getRandomInt();
            int g = getRandomInt();
            int b = getRandomInt();
            for (int i = 0; i < data.getRow().size(); i++) {
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(data.getRow().get(i));
//                setCellBackgroundColor(wb,cell,r,g,b);
            }
        }
    }

    /**
     * 生成0-255的整数
     *
     * @return
     */
    public static int getRandomInt() {
        Random ra = new Random();
        return ra.nextInt(256);
    }

    /**
     * 设置单元格背景颜色
     *
     * @param wb
     * @param cell
     * @param r
     * @param g
     * @param b
     */
    private static void setCellBackgroundColor(XSSFWorkbook wb, XSSFCell cell, int r, int g, int b) {
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(new XSSFColor(new Color(r, g, b)));
        cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(cellStyle);
    }

    public static void main(String[] args) throws IOException {
        TreeGridVo treeGridVo = new TreeGridVo();
        treeGridVo.setRow(Arrays.asList("标题1", "标题2", "标题3", "标题4", "标题5", "标题6"));
//        treeGridVo.setRow(Arrays.asList("123","23","2","34","4","6"));
        TreeGridVo tree1 = new TreeGridVo();
        tree1.setRow(Arrays.asList("1", "1", "1", "1", "1", "1"));
        TreeGridVo tree2 = new TreeGridVo();
        tree2.setRow(Arrays.asList("1", "1", "1", "1", "1", "1"));
        TreeGridVo tree11 = new TreeGridVo();
        tree11.setRow(Arrays.asList("2", "2", "2", "2", "2", "2"));
        TreeGridVo tree12 = new TreeGridVo();
        tree12.setRow(Arrays.asList("2", "2", "2", "2", "2", "2"));
        TreeGridVo tree121 = new TreeGridVo();
        tree121.setRow(Arrays.asList("3", "3", "3", "3", "3", "3"));
        tree12.setChildren(Arrays.asList(tree121));
        tree1.setChildren(Arrays.asList(tree11, tree12));
        TreeGridVo tree211 = new TreeGridVo();
        tree211.setRow(Arrays.asList("3", "3", "3", "3", "3", "3"));
        TreeGridVo tree21 = new TreeGridVo();
        tree21.setRow(Arrays.asList("2", "2", "2", "2", "2", "2"));
        tree21.setChildren(Arrays.asList(tree211));
        tree2.setChildren(Arrays.asList(tree21));
        treeGridVo.setChildren(Arrays.asList(tree1, tree2));

        export(treeGridVo, "C:\\Users\\likun\\Desktop\\excel.xlsx");
    }
}
